package com.gfwdhh.algorithms.first.twosum;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by gfwdhh on 2017-12-26.
 */
public class TwoSum {
//数组初始化以及赋值
    public int[] twoSum(int[] nums, int target) {
        int[] result={-1,-1};
        for(int i=0;i<nums.length;i++){
            int nums_i=nums[i];
            for(int j=0;j<nums.length;j++){
                int nums_j=nums[j];
                if(i!=j&&(nums_i+nums_j==target)){
                    int[] a={i,j};
                    return a;
                }
            }
        }
        return result;
    }

//    hashMap的包含判断以及加入值以及下标
    public int[] twoSumHash(int[] nums, int target) {
        Map<Integer,Integer> map=new HashMap<Integer,Integer>();
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(target-nums[i])&&map.get(target-nums[i])!=i){
                return new int[]{map.get(target-nums[i]),i};
            }else{
                map.put(nums[i],i);
            }
        }
        for(int i=0;i<nums.length;i++){
            if(map.containsKey(target-nums[i])&&map.get(target-nums[i])!=i){
                return new int[]{map.get(target-nums[i]),i};
            }
        }
        return null;

    }

    public static void main(String[] args) {

    }
}
